package com.btpn.custody.dao.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.btpn.custody.dao.IBranch;
import com.btpn.custody.entity.Branch;

@Repository("branchDaoHibernate")
public class BranchDAO implements IBranch {
	
	protected HibernateTemplate template = null;
	
	@Autowired @Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		template = new HibernateTemplate(sessionFactory);
	}	

	@Override
	public Branch get(Long id) {
		return template.get(Branch.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Branch> findAll() {
//		return template.loadAll(Branch.class);
		return template.findByNamedQuery(Branch.NQ__FIND_ALL);
	}
	
	@Override
	public Integer getCount() {
		return Integer.valueOf(findAll().size());
	}
	
	@Override
	public Serializable create(Branch branch) {
		return template.save(branch);
	}

	@Override
	public void createOrUpdate(Branch branch) {
		template.saveOrUpdate(branch);
	}
	
	@Override
	public void update(Branch branch) {
		template.update(branch);
	}

	@Override
	public void delete(Long id) {
		template.delete(get(id));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Branch getByCode(String branchCode) {
		List<Branch> result = template.findByNamedQueryAndNamedParam(Branch.NQ__GET_BY__CODE, "branchCode", branchCode);
		
		if (result.size()>0) {
			return result.get(0);
		} else {
			return null;
		}		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Branch getByName(String branchName) {
		List<Branch> result = template.findByNamedQueryAndNamedParam(Branch.NQ__GET_BY__NAME, "branchName", branchName);
		
		if (result.size()>0) {
			return result.get(0);
		} else {
			return null;
		}	
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Branch> findByAreaId(Long areaId) {
		
		return template.findByNamedQueryAndNamedParam(Branch.NQ__FIND_BY__AREA_ID, "areaId", areaId);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Branch> findNotById(Long id, Long from, Long to) {
		// get all list that != id
		List<Branch> list = template.findByNamedQueryAndNamedParam(Branch.NQ__FIND_NOT_BY__ID, "id", id);

		return list.subList(Integer.valueOf(from.intValue()), Integer.valueOf(to.intValue()));
	}

//	@SuppressWarnings("unchecked")
//	@Override
//	public List<Branch> findByCriteria(DetachedCriteria dc, Integer from, Integer size) {
//		try {
//			Criteria criteria = dc.getExecutableCriteria(template.getSessionFactory().getCurrentSession());
//			criteria.setFirstResult(from);
//			criteria.setMaxResults(size);
//			return criteria.list();
//		} catch (HibernateException e) {
//			throw e;
//		}
//	}	
}